package com.tz.crm.config;

import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.util.ArrayList;
import java.util.List;

/**
 * SpringMVC Configuration
 */
@Configuration
@Import(AppConfig.class)
@ComponentScan({"com.tz.crm.controller", "com.tz.crm.advice"})
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    /**
     * MessageSource
     *
     * @return MessageSource
     */
    @Bean
    public MessageSource validationMessageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        //字符编码
        messageSource.setDefaultEncoding("UTF-8");
        //Set an array of basenames
        messageSource.setBasenames("validationMessages");
        //whether to use the message code as default message
        messageSource.setUseCodeAsDefaultMessage(true);
        return messageSource;
    }

    /**
     * JSR303验证Validator
     */
    @Override
    public Validator getValidator() {
        LocalValidatorFactoryBean lvfb = new LocalValidatorFactoryBean();
        lvfb.setValidationMessageSource(validationMessageSource());
        return lvfb;
    }

    /**
     * InternalResourceViewResolver
     *
     * @return InternalResourceViewResolver
     */
    @Bean
     public InternalResourceViewResolver getInternalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        return resolver;
    }

    /**
     * Configure a handler to delegate unhandled requests
     * by forwarding to the Servlet container's "default" servlet,
     * such as : /
     */
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    /**
     * mapping classpath resources to public static resources
     * such as : images,js,css
     */
    @Override
     public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/images/**").addResourceLocations("classpath:/public/images/");
        registry.addResourceHandler("/css/**").addResourceLocations("classpath:/public/css/");
        registry.addResourceHandler("/js/**").addResourceLocations("classpath:/public/js/");
    }

    /**
     * Dozer Mapper
     *
     * @return Mapper
     */
    @Bean
    public Mapper mapper() {
        List<String> list = new ArrayList<>();
        list.add("dozerBeanMapping.xml");
        return new DozerBeanMapper(list);
    }

    /**
     * 当接收到含有上传文件的请求时，
     * 会自动调用该组件进行解析。
     * 解析的结果文件保存为 MultipartFile，可以作为控制器方法的参数直接接收。
     * (底层是调用的 commons-fileupload 组件提供的功能)
     *
     * @return 自动调用该组件进行解析。
     */
    @Bean
    public MultipartResolver multipartResolver() {
        MultipartResolver multipartResolver = new CommonsMultipartResolver();
        return multipartResolver;
    }

}
